{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from outlier.STLWithMedianRemoval import *\n",
      "import pandas as pd\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from pyculiarity import detect_ts"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "twitter_example_data = pd.read_csv('data/vc_3.json_remake',\n",
      "                                    usecols=['timestamp', 'points'])\n",
      "twitter_example_data.points.interpolate(inplace=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results = detect_ts(twitter_example_data,\n",
      "                max_anoms=0.02, \n",
      "                direction='both')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 55
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = pd.read_csv('data/vc_3.json_remake',index_col=0,parse_dates=True)\n",
      "data.points.interpolate(inplace=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 22
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = pd.DataFrame(index=np.arange(data.shape[0]), columns=[\"timestamp\",\"points\"])\n",
      "b['timestamp'] = data.index\n",
      "b['points'] = data.points.tolist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "twitter_example_data['timestamp'] = pd.to_datetime(twitter_example_data['timestamp'])\n",
      "b = twitter_example_data.set_index('timestamp', drop=True)\n",
      "\n",
      "b['anomaly'] = np.ones(b.shape[0])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 46
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print b.ix[anoms.index[0]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>anoms</th>\n",
        "      <th>timestamp</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>timestamp</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>2016-04-14 02:09:00</th>\n",
        "      <td>946.951228</td>\n",
        "      <td>2016-04-14 02:09:00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016-04-14 02:10:00</th>\n",
        "      <td>1000.499162</td>\n",
        "      <td>2016-04-14 02:10:00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016-04-14 02:11:00</th>\n",
        "      <td>977.897871</td>\n",
        "      <td>2016-04-14 02:11:00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016-04-14 02:12:00</th>\n",
        "      <td>969.528083</td>\n",
        "      <td>2016-04-14 02:12:00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016-04-14 02:13:00</th>\n",
        "      <td>878.021218</td>\n",
        "      <td>2016-04-14 02:13:00</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "</div>"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 56,
       "text": [
        "                           anoms           timestamp\n",
        "timestamp                                           \n",
        "2016-04-14 02:09:00   946.951228 2016-04-14 02:09:00\n",
        "2016-04-14 02:10:00  1000.499162 2016-04-14 02:10:00\n",
        "2016-04-14 02:11:00   977.897871 2016-04-14 02:11:00\n",
        "2016-04-14 02:12:00   969.528083 2016-04-14 02:12:00\n",
        "2016-04-14 02:13:00   878.021218 2016-04-14 02:13:00"
       ]
      }
     ],
     "prompt_number": 56
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "aidx = pd.to_datetime(anoms.timestamp)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 59
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "aidx"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 61,
       "text": [
        "timestamp\n",
        "2016-04-14 02:09:00   2016-04-14 02:09:00\n",
        "2016-04-14 02:10:00   2016-04-14 02:10:00\n",
        "2016-04-14 02:11:00   2016-04-14 02:11:00\n",
        "2016-04-14 02:12:00   2016-04-14 02:12:00\n",
        "2016-04-14 02:13:00   2016-04-14 02:13:00\n",
        "2016-04-14 02:14:00   2016-04-14 02:14:00\n",
        "2016-04-14 02:15:00   2016-04-14 02:15:00\n",
        "2016-04-14 02:16:00   2016-04-14 02:16:00\n",
        "2016-04-14 02:17:00   2016-04-14 02:17:00\n",
        "2016-04-14 02:18:00   2016-04-14 02:18:00\n",
        "2016-04-14 02:19:00   2016-04-14 02:19:00\n",
        "2016-04-14 02:20:00   2016-04-14 02:20:00\n",
        "2016-04-14 02:21:00   2016-04-14 02:21:00\n",
        "2016-04-14 02:22:00   2016-04-14 02:22:00\n",
        "2016-04-14 02:23:00   2016-04-14 02:23:00\n",
        "2016-04-14 02:24:00   2016-04-14 02:24:00\n",
        "2016-04-14 02:25:00   2016-04-14 02:25:00\n",
        "2016-04-20 08:55:00   2016-04-20 08:55:00\n",
        "2016-04-20 08:59:00   2016-04-20 08:59:00\n",
        "2016-04-20 09:02:00   2016-04-20 09:02:00\n",
        "2016-04-20 09:03:00   2016-04-20 09:03:00\n",
        "2016-04-20 09:04:00   2016-04-20 09:04:00\n",
        "2016-04-20 09:05:00   2016-04-20 09:05:00\n",
        "2016-04-21 08:42:00   2016-04-21 08:42:00\n",
        "2016-04-21 08:43:00   2016-04-21 08:43:00\n",
        "2016-04-23 03:00:00   2016-04-23 03:00:00\n",
        "2016-04-23 18:39:00   2016-04-23 18:39:00\n",
        "2016-04-24 17:29:00   2016-04-24 17:29:00\n",
        "2016-04-24 20:39:00   2016-04-24 20:39:00\n",
        "2016-04-25 04:56:00   2016-04-25 04:56:00\n",
        "                              ...        \n",
        "2016-05-03 05:23:00   2016-05-03 05:23:00\n",
        "2016-05-03 05:24:00   2016-05-03 05:24:00\n",
        "2016-05-03 05:25:00   2016-05-03 05:25:00\n",
        "2016-05-03 05:26:00   2016-05-03 05:26:00\n",
        "2016-05-03 05:27:00   2016-05-03 05:27:00\n",
        "2016-05-03 05:28:00   2016-05-03 05:28:00\n",
        "2016-05-03 05:29:00   2016-05-03 05:29:00\n",
        "2016-05-03 05:30:00   2016-05-03 05:30:00\n",
        "2016-05-03 05:31:00   2016-05-03 05:31:00\n",
        "2016-05-03 05:32:00   2016-05-03 05:32:00\n",
        "2016-05-03 05:33:00   2016-05-03 05:33:00\n",
        "2016-05-03 05:34:00   2016-05-03 05:34:00\n",
        "2016-05-03 05:35:00   2016-05-03 05:35:00\n",
        "2016-05-03 05:36:00   2016-05-03 05:36:00\n",
        "2016-05-03 05:37:00   2016-05-03 05:37:00\n",
        "2016-05-03 05:38:00   2016-05-03 05:38:00\n",
        "2016-05-03 05:40:00   2016-05-03 05:40:00\n",
        "2016-05-03 05:41:00   2016-05-03 05:41:00\n",
        "2016-05-03 06:06:00   2016-05-03 06:06:00\n",
        "2016-05-03 06:08:00   2016-05-03 06:08:00\n",
        "2016-05-03 06:11:00   2016-05-03 06:11:00\n",
        "2016-05-03 06:38:00   2016-05-03 06:38:00\n",
        "2016-05-03 06:43:00   2016-05-03 06:43:00\n",
        "2016-05-03 13:41:00   2016-05-03 13:41:00\n",
        "2016-05-03 13:45:00   2016-05-03 13:45:00\n",
        "2016-05-03 13:48:00   2016-05-03 13:48:00\n",
        "2016-05-03 13:51:00   2016-05-03 13:51:00\n",
        "2016-05-03 13:52:00   2016-05-03 13:52:00\n",
        "2016-05-03 13:53:00   2016-05-03 13:53:00\n",
        "2016-05-04 03:45:00   2016-05-04 03:45:00\n",
        "Name: timestamp, dtype: datetime64[ns]"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "twitter_example_data['timestamp'] = pd.to_datetime(twitter_example_data['timestamp'])\n",
      "twitter_example_data.set_index('timestamp', drop=True)\n",
      "\n",
      "# make a nice plot\n",
      "f, ax = plt.subplots(1, 1)\n",
      "ax.plot(twitter_example_data['timestamp'], twitter_example_data['value'], 'b')\n",
      "ax.plot(results['anoms'].index, results['anoms']['anoms'], 'ro')\n",
      "ax.set_title('Detected Anomalies')\n",
      "ax.set_ylabel('Count')\n",
      "f.tight_layout()\n",
      "#  twitter_example_data['timestamp'] = pd.to_datetime(twitter_example_data['timestamp'])\n",
      "#     twitter_example_data.set_index('timestamp', drop=True)\n",
      "\n",
      "#     # make a nice plot\n",
      "#     f, ax = plt.subplots(2, 1, sharex=True)\n",
      "#     ax[0].plot(twitter_example_data['timestamp'], twitter_example_data['count'], 'b')\n",
      "#     ax[0].plot(results['anoms'].index, results['anoms']['anoms'], 'ro')\n",
      "#     ax[0].set_title('Detected Anomalies')\n",
      "#     ax[1].set_xlabel('Time Stamp')\n",
      "#     ax[0].set_ylabel('Count')\n",
      "#     ax[1].plot(results['anoms'].index, results['anoms']['anoms'], 'b')\n",
      "#     ax[1].set_ylabel('Anomaly Magnitude')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 71
    }
   ],
   "metadata": {}
  }
 ]
}